package com.csht.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csht.model.CshtUser;
import com.csht.service.LoginService;
import com.csht.util.MD5;
import com.csht.util.ValidateCode;

/**
 * 登录
 * @author chenhuiyan
 *
 */
@Controller
public class LoginController {
	
	@Autowired
	private LoginService loginService;
	
	/**
	 * 登录页面
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/login")
	public String login(ModelMap model) throws Exception {
		
		return "/login/login";
	}
	
	/**
	 * 涮新验证码
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/getValidationCode")
	public void getValidationCode(HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		ValidateCode alidateCode = new ValidateCode();
		alidateCode.doGet(request, response);
		HttpSession session = request.getSession();
		String code = (String) session.getAttribute("validation_code");
		System.out.println(code);
	}
	
	/**
	 * 根据用户名或手机号查找用户,是否需要认证码登录
	 * @param usernameOrTel
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/getUserLoginFailTime")
	public Map<String, Object> getUserLoginFailTime(@RequestParam(name = "username") String usernameOrTel) 
			throws Exception{
		Map<String, Object> messages = new HashMap<String, Object>();
		messages.put("message", false);
		CshtUser cshtUser = loginService.getUserByUsernameOrTel(usernameOrTel);
		if(cshtUser != null) {
			if(cshtUser.getUserLoginFailTime() >= 4) {
				messages.put("message", true);
			}
		}
		return messages;
	}
	
	/**
	 * 用户登录
	 * @param username
	 * @param password
	 * @param code
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/userLogin")
	public String userLogin(@RequestParam(name = "username") String username, 
			@RequestParam(name = "password") String password, @RequestParam(name = "code") String code, 
			HttpServletRequest request, ModelMap model) throws Exception{
		String validation_code = request.getSession().getAttribute("validation_code").toString(); //获取系统生成的验证码
		if("no".equals(code)) { //无验证码时,进行用户查询
			CshtUser cshtUser = loginService.getUserByUsernameOrTel(username); //查找用户
			if(cshtUser != null) { //用户存在时，进行账号是否封停校验
				if("否".equals(cshtUser.getUserStopSeal())) { //账号没有封停，进行是否需要输入验证码校验
					if(cshtUser.getUserLoginFailTime() >= 4) { //密码失败大于等于4次时需要输入验证码
						model.addAttribute("needCodeBool", "f"); //需要输入验证码,登录不通过
						System.out.println("需要输入验证码,登录不通过");
						model.addAttribute("username", username); //保存用户输入的用户名
						model.addAttribute("password", password); //保存用户输入的密码
						return "/login/login";
					}else { //不需要验证码时。进行密码校验
						if(cshtUser.getUserPassword().equals(MD5.getMD5Code(password))) {//密码正确时，登录成功
							request.getSession().setAttribute("loginUser", cshtUser); //保存用户
							cshtUser.setUserLoginFailTime(0); //更新登录失败次数为0
							loginService.updateLoginFailTime(cshtUser); //更新登录失败次数为0
							return "redirect:/index";
						}else {//密码不正确
							model.addAttribute("passwordBool", "f"); //密码不正确,登录不通过
							System.out.println("密码不正确,登录不通过");
							cshtUser.setUserLoginFailTime(cshtUser.getUserLoginFailTime() + 1);
							loginService.updateLoginFailTime(cshtUser); //更新登录失败次数
							return "/login/login";
						}
					}
				}else { //账号封停
					model.addAttribute("stopSealBool", "f"); //账号封停,登录不通过
					System.out.println("账号封停,登录不通过");
					return "/login/login";
				}
			}else { //用户不存在
				model.addAttribute("usernameBool", "f"); //账号不存在,登录不通过
				System.out.println("账号不存在,登录不通过");
				return "/login/login";
			}
		}else { //需要输入验证吗时
			if(code.equalsIgnoreCase(validation_code)) { //先进行验证码校验,通过
				CshtUser cshtUser = loginService.getUserByUsernameOrTel(username); //查找用户
				if(cshtUser != null) { //用户存在时，进行账号是否封停校验
					if("否".equals(cshtUser.getUserStopSeal())) { //账号没有封停，进行是否需要输入验证码校验
						if(cshtUser.getUserPassword().equals(MD5.getMD5Code(password))) {//密码正确时，登录成功
							request.getSession().setAttribute("loginUser", cshtUser); //保存用户
							cshtUser.setUserLoginFailTime(0); //更新登录失败次数为0
							loginService.updateLoginFailTime(cshtUser); //更新登录失败次数为0
							return "redirect:/index";
						}else {//密码不正确
							model.addAttribute("passwordBool", "f"); //密码不正确,登录不通过
							System.out.println("密码不正确,登录不通过");
							cshtUser.setUserLoginFailTime(cshtUser.getUserLoginFailTime() + 1);
							loginService.updateLoginFailTime(cshtUser); //更新登录失败次数
							return "/login/login";
						}
					}else { //账号封停
						model.addAttribute("stopSealBool", "f"); //账号封停,登录不通过
						System.out.println("账号封停,登录不通过");
						return "/login/login";
					}
				}else { //用户不存在
					model.addAttribute("usernameBool", "f"); //账号不存在,登录不通过
					System.out.println("账号不存在,登录不通过");
					return "/login/login";
				}
			}else { //验证码校验不通过
				model.addAttribute("codeBool", "f"); //验证码错误,登录不通过
				System.out.println("验证码错误,登录不通过");
				model.addAttribute("username", username); //保存用户输入的用户名
				model.addAttribute("password", password); //保存用户输入的密码
				return "/login/login";
			}
		}
	}
	

}
